<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 6/25/21
 * Time: 5:23 PM
 */

namespace app\api\controller\v1;

// 提现模块
use app\api\model\Store as StoreModel;
use app\api\model\StoreUser;
use app\api\model\StoreUser as StoreUserModel;
use app\api\model\UserMoney;
use app\api\model\UserMoney as UserMoneyModel;

class Withdrawal
{
    // 根据门店查询提现金额
    public function getWithdrawalByStore($start_time,$end_time)
    {
        $where = [];
        if(!$start_time){
            $start_time = strtotime("-1 month");
            $end_time = time();
            $where[] = ['create_time', 'egt', $start_time];
            $where[] = ['create_time', 'elt', $end_time];
        } else {
            $where[] = ['create_time', 'egt', strtotime($start_time." 00:00:00")];
            $where[] = ['create_time', 'elt', strtotime($end_time." 23:59:59")];
        }
        $store = StoreModel::order('id desc')->select()->toArray();
        $money = 0;
        foreach ($store as $k=>$v) {
            $storeUser = StoreUser::where('store_id',$store[$k]['id'])->select()->toArray();
            $user_ids = array_column($storeUser, 'user_id');
            $whereOne = $where;
            $whereOne[] = ['user_id','in',$user_ids];
            $whereOne[] = ['type','eq',2];
            $store[$k]['user_money'] = UserMoney::where($whereOne)->sum('money');
            $money = $money + $store[$k]['user_money'];
        }
        $whereTwo = $where;
        $whereTwo[] = ['type','eq',2];
        $total = UserMoney::where($whereTwo)->sum('money');
        array_push($store,[ 'name'=>'顾客总提现', 'user_money'=> ($total - $money),'id' => -1]);
        if(count($store) > 0) {
            $store = arraySort($store,'user_money');
        }
        return $store;
    }


    // 查询门店的提现列表
    public function getWithdrawalByMoney($start_time,$end_time,$id, $store_id)
    {
        $where = [];
        if(!$start_time){
            $start_time = strtotime("-1 month");
            $end_time = time();
            $where[] = ['user_money.create_time', 'egt', $start_time];
            $where[] = ['user_money.create_time', 'elt', $end_time];
        } else {
            $where[] = ['user_money.create_time', 'egt', strtotime($start_time." 00:00:00")];
            $where[] = ['user_money.create_time', 'elt', strtotime($end_time." 23:59:59")];
        }
        if($id != -1) {
            $storeUsers = StoreUserModel::where('store_id',$id)
                ->select()->toArray();
            $ids = array_column($storeUsers,'user_id');
            $whereOne = $where;
            $whereOne[] = ['user_id','in',$ids];
            $whereOne[] = ['type','eq',2];
            $list = UserMoneyModel::where($whereOne)->with('user')
                ->order('create_time desc')
                ->select()->each(function ($item){
                    $item['store_user'] = StoreUserModel::where('user_id',$item['user_id'])->find();
                })->toArray();
            $sum = UserMoneyModel::where($whereOne)->sum('money');
            return [
                'data' => $list,
                'sum' => $sum,
            ];
        } else {
            $whereOne = $where;
            $whereOne[] = ['user_type','eq',1];
            $whereOne[] = ['user_money.type','eq',2];
            $sum = 0;
            $list = UserMoneyModel::hasWhere('user',function($query) use($store_id){
                if($store_id) {
                    $query->where('store_id','eq',$store_id);
                }
            })->where($whereOne)->with('user.store')
                ->order('create_time desc')
                ->select()->each(function ($item){
                    $item['store_user'] = null;
                })->toArray();
            foreach ($list as $k=>$v){
                $sum += $list[$k]['money'];
            }
            return [
                'data' => $list,
                'sum' => $sum,
            ];
        }
    }
}